import { IPSModelObject } from '@ibiz/dynamic-model-api';
import { IContext, IParams } from '../../core';
import { FrameworkDelegate } from '../../model-core';
import { StoreBase } from '../base/store-base';

/**
 * 全局缓存数据
 *
 * @author chitanda
 * @date 2021-05-12 19:05:19
 * @export
 * @class GlobalStoreData
 */
export class GlobalStoreData {
  /**
   * 框架组件绘制代理
   *
   * @author chitanda
   * @date 2021-05-12 20:05:30
   * @type {FrameworkDelegate}
   */
  delegate: FrameworkDelegate = null;
  /**
   * 视图模型
   *
   * @author chitanda
   * @date 2021-05-12 19:05:30
   * @type {IPSAppView}
   */
  model: IPSModelObject = null;
  /**
   * 上下文参数
   *
   * @author chitanda
   * @date 2021-05-12 20:05:43
   * @type {IContext}
   */
  context: IContext = {};
  /**
   * 额外参数
   *
   * @author chitanda
   * @date 2021-05-12 20:05:40
   * @type {IParams}
   */
  params: IParams = {};
}

/**
 * 全局缓存
 *
 * @export
 * @class GlobalStore
 * @extends {StoreBase}
 */
export class GlobalStore extends StoreBase<GlobalStoreData> {
  private static readonly instance = new GlobalStore();

  /**
   * Creates an instance of GlobalStore.
   * @author chitanda
   * @date 2021-05-12 19:05:52
   */
  constructor() {
    if (GlobalStore.instance) {
      return GlobalStore.instance;
    }
    super();
  }

  protected getState() {
    return new GlobalStoreData();
  }

  static getInstance(): GlobalStore {
    return this.instance;
  }
}
